package com.drama.api.sys.company.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.drama.api.base.Constants;
import com.drama.api.base.Type;
import com.drama.api.sys.company.dao.SysCompanyDao;
import com.drama.api.sys.company.dao.SysCompanyFlowDao;
import com.drama.api.sys.company.domain.po.SysCompanyFlowPO;
import com.drama.api.sys.company.domain.po.SysCompanyPO;
import com.drama.api.sys.company.domain.vo.*;
import com.drama.api.sys.company.service.ISysCompanyFlowService;
import com.drama.api.sys.log.dao.SysLogDao;
import com.drama.api.task.SysLogThread;
import com.drama.exception.BusinessException;
import com.drama.util.ToolUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;


@Service
@Transactional
@Slf4j
public class SysCompanyFlowServiceImpl implements ISysCompanyFlowService {

    @Autowired
    private SysCompanyFlowDao sysCompanyFlowDao;
    @Autowired
    private SysCompanyDao sysCompanyDao;
    @Autowired
    private SysLogDao sysLogDao;


    @Override
    public List<SysCompanyFlowLogVO> getList(SysCompanyFlowLogSearchVO searchVO) {
        return sysCompanyFlowDao.getList(searchVO);
    }

    @Override
    public List<SysCompanyFlowLogVO> getFlow(List<SysCompanyFlowLogVO> list) {
        for(SysCompanyFlowLogVO companyFlowLogVO:list){
            companyFlowLogVO.setFlowNum(ToolUtil.formatFileSize(companyFlowLogVO.getFlowNum()));
        }
        return list;
    }

    @Override
    public List<SysCompanyFlowVO> getCompanyList(SysCompanyFlowSearchVO searchVO) {
        return sysCompanyDao.getCompanyFlowList(searchVO);
    }

    @Override
    public List<SysCompanyFlowVO> getCompanyFlow(List<SysCompanyFlowVO> list) {
        for(SysCompanyFlowVO companyFlowLogVO:list){
            String s="";
            if(new BigDecimal(companyFlowLogVO.getFlowNum()).compareTo(BigDecimal.ZERO)==-1){
                s="-";
            }
            companyFlowLogVO.setFlowNum(s+ToolUtil.formatFileSize(new BigDecimal(companyFlowLogVO.getFlowNum()).abs().toString()));
//            companyFlowLogVO.setFlowNum(ToolUtil.formatFileSize(companyFlowLogVO.getFlowNum()));
        }
        return list;
    }

    @Override
    public void op(SysCompanyFlowLogOpVO opVO) {
        SysCompanyPO sysCompanyPO=sysCompanyDao.selectByPrimaryKey(opVO.getCompanyId());
        if(sysCompanyPO==null){
            throw new BusinessException(Type.NOT_FOUND_ERROR);
        }
        SysCompanyFlowPO companyFlowPO=new SysCompanyFlowPO();
        companyFlowPO.preInsert();
        companyFlowPO.setCompanyId(sysCompanyPO.getId());
        companyFlowPO.setFlowState(opVO.getFlowState());
        companyFlowPO.setFlowNum(opVO.getFlowNum());
        int i = sysCompanyFlowDao.insertSelective(companyFlowPO);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
        if(opVO.getFlowState()==1){//分配
            sysCompanyPO.setFlowNum(new BigDecimal(sysCompanyPO.getFlowNum()).add(new BigDecimal(opVO.getFlowNum())).toString());
            new SysLogThread(sysLogDao,Constants.SYS_LOG_MODULE[1],"分配商户流量，商户ID:"+sysCompanyPO.getId(), (String) StpUtil.getLoginId()).start();
        }
        if(opVO.getFlowState()==2){//扣除
            sysCompanyPO.setFlowNum(new BigDecimal(sysCompanyPO.getFlowNum()).subtract(new BigDecimal(opVO.getFlowNum())).toString());
            new SysLogThread(sysLogDao,Constants.SYS_LOG_MODULE[1],"扣除商户流量，商户ID:"+sysCompanyPO.getId(), (String) StpUtil.getLoginId()).start();
        }
        sysCompanyPO.preUpdate();
        int j = sysCompanyDao.updateByPrimaryKeySelective(sysCompanyPO);
        if (j == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }
}